<?php

declare(strict_types=1);

namespace App\CustHub\Controller;

use App\Common\Constants\ErrorCode;
use App\Exception\BusinessException;
use Mine\MineController;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\Validation\Contract\ValidatorFactoryInterface;

class BaseController extends MineController
{
    #[Inject]
    protected ValidatorFactoryInterface $validationFactory;
    /**
     * 不需要登录的方法
     * @var array
     */
    public array $noNeedLogin = [];

    /**
     * 不需要验证签名的方法
     * @var array
     */
    public array $noNeedVerifySign = [];


    public function validateParam(array $valid_method = [],array $message = [],$code = ErrorCode::PARAMS_ERROR)
    {
        $validator = $this->validationFactory->make(
            $this -> request -> all(),
            $valid_method,
            $message
        );

        if ($validator->fails()){
            // Handle exception
            $errorMessage = $validator->errors()->first();
            $validator -> errors();
            throw new BusinessException($code,$errorMessage);
        }
    }
}